Skip to content

remote_files: Add support for directly using local cache - #87

Merged
joverlee521 merged 2 commits into
mainfrom
use-local-cache
Oct 13, 2026
Merged

joverlee521 merged 2 commits into
mainfrom
use-local-cache

Conversation

@joverlee521

@joverlee521 joverlee521 commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Description of proposed changes

The Snakemake storage usually checks the existence and revalidates remote files before using the local cache, which can lead to errors when running the workflow offline even if the local cache already exists.

This change adds support to directly use the local cache if it exists and bypass the remote file check when

  1. the config parameter use_remote_files is False
  2. the environment variable NEXTSTRAIN_REMOTE_FILES is False
  3. the workflow is running offline

Based on discussions in nextstrain/public#47 and changes tested in measles repo
nextstrain/measles@60634e2...b238682

Related issue(s)

Part of nextstrain/public#47

Checklist

The Snakemake storage usually checks the existence and revalidates remote
files before using the local cache, which can lead to errors when running
the workflow offline even if the local cache already exists.

This change adds support to directly use the local cache if it exists
and bypass the remote file check when
1. the config parameter `use_remote_files` is False
2. the environment variable `NEXTSTRAIN_REMOTE_FILES` is False
3. the workflow is running offline

Based on discussions in <nextstrain/public#47>
and changes tested in measles repo
<nextstrain/measles@60634e2...b238682>

@jameshadfield jameshadfield left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on and turning the prototype into a polished PR! I haven't tested the code but reading through it I think the behaviour is spot on. One minor query re: env variables is the only thing that stuck out to me.

Comment thread snakemake/remote_files.smk Outdated
Comment on lines +17 to +24
def is_online(host="8.8.8.8", port=53, timeout=3) -> bool:
"""
Check if workflow has network connection.
Based on <https://stackoverflow.com/a/33117579>
Host: 8.8.8.8 (google-public-dns-a.google.com)
OpenPort: 53/tcp
Service: domain (DNS/TCP)
"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! I like this general "is online" approach better than the per-source checks in my original prototype

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, by checking against Google, is_online could return True when sources are unavailable, or False when sources are available but Google is down. This is very edge case-y though, I think it's fine to keep as-is.

@joverlee521

Copy link
Copy Markdown
Contributor Author

Will merge this next Tuesday to monitor downstream uses.

@joverlee521
joverlee521 merged commit 6e82a95 into main Oct 13, 2026
2 checks passed
@joverlee521
joverlee521 deleted the use-local-cache branch October 13, 2026 18:20
Comment on lines +232 to +237
if not local.exists():
raise Exception(
f"Unable to find local cached file for {so.query!r}. "
"Please make sure you have run the workflow at least once with "
"internet connection and config.use_remote_files set to True to "
"to fetch and save the remote file locally.")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I should have tested more with wildcards. This leads to an error when the inputs are defined with wildcards, e.g. rsv.

The local path .snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/{a_or_b}/metadata.tsv.gz does not exist which leads to this exception:

Exception in file "/nextstrain/build/shared/vendored/snakemake/remote_files.smk", line 233:
Unable to find local cached file for 'https://data.nextstrain.org/files/workflows/rsv/{a_or_b}/metadata.tsv.gz'. Please make sure you have run the workflow at least once with internet connection and config.use_remote_files set to True to to fetch and save the remote file locally.
  File "/nextstrain/build/workflow/snakemake_rules/merge_inputs.smk", line 55, in <module>
  File "/nextstrain/build/workflow/snakemake_rules/merge_inputs.smk", line 53, in _gather_inputs
  File "/nextstrain/build/workflow/snakemake_rules/merge_inputs.smk", line 53, in <dictcomp>
  File "/nextstrain/build/workflow/snakemake_rules/merge_inputs.smk", line 53, in <dictcomp>
  File "/nextstrain/build/shared/vendored/snakemake/remote_files.smk", line 185, in path_or_url
  File "/nextstrain/build/shared/vendored/snakemake/remote_files.smk", line 233, in _local_or_storage

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've relied on the Snakemake inputs to automatically expand wildcards so path_or_url does not expand the wildcards. If we continue with that pattern and just remove this custom error, then the user will see the warning logs and the usual MissingInputsException

Using the cached copy of https://data.nextstrain.org/files/workflows/rsv/{a_or_b}/metadata.tsv.gz at .snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/{a_or_b}/metadata.tsv.gz without revalidation. Set config.use_remote_files to True if you want to force revalidation.
Using the cached copy of https://data.nextstrain.org/files/workflows/rsv/{a_or_b}/sequences.fasta.xz at .snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/{a_or_b}/sequences.fasta.xz without revalidation. Set config.use_remote_files to True if you want to force revalidation.
Using the cached copy of https://data.nextstrain.org/files/workflows/rsv/{a_or_b}/metadata_restricted.tsv.gz at .snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/{a_or_b}/metadata_restricted.tsv.gz without revalidation. Set config.use_remote_files to True if you want to force revalidation.
Using the cached copy of https://data.nextstrain.org/files/workflows/rsv/{a_or_b}/sequences_restricted.fasta.xz at .snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/{a_or_b}/sequences_restricted.fasta.xz without revalidation. Set config.use_remote_files to True if you want to force revalidation.
Assuming unrestricted shared filesystem usage.
host: 7cd889771091
Building DAG of jobs...
MissingInputException in rule merge_metadata in file "/nextstrain/build/workflow/snakemake_rules/merge_inputs.smk", line 85:
Missing input files for rule merge_metadata:
    output: results/a/metadata.tsv
    wildcards: a_or_b=a
    affected files:
        .snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/a/metadata.tsv.gz
        .snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/a/metadata_restricted.tsv.gz

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing exception in #88

@joverlee521 joverlee521 mentioned this pull request Oct 14, 2026
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants